﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FisheryPlatform.Entity;
using FisheryPlatform.Bussiness.ShareCode;

namespace FisheryPlatform.Web.Admin.ShareCode
{
    public partial class ClassAdd : System.Web.UI.Page
    {
        protected int ResultNum = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            if (this.IsPostBack)
            {
                Save();
            }
            else
            {
                LoadData();
            }
            #endregion
        }

        private void LoadData()
        {
            #region
            this.txt_ClassId.Value = Request.QueryString["cid"] ?? "0";
            long classId = 0;
            if (long.TryParse(this.txt_ClassId.Value, out classId) && classId > 0)
            {
                ShareCodeClassEntity classInfo = ShareCodeManager.GetByShareCodeClassId(classId);
                this.txt_Name.Value = classInfo.Name;
                this.txt_NameEN.Value = classInfo.NameEN;
                this.txt_Code.Value = classInfo.Code;

                this.txt_Name.Disabled = true;
            }
            #endregion
        }

        private void Save()
        {
            #region
            if (string.IsNullOrEmpty(txt_Name.Value))
            {
                ResultNum = 2;
                return;
            }
            long classId = 0;
            long.TryParse(this.txt_ClassId.Value, out classId);
            ShareCodeClassEntity classInfo = new ShareCodeClassEntity();
            classInfo.ClassID = classId;
            classInfo.Name = txt_Name.Value.Trim();
            classInfo.NameEN = txt_NameEN.Value.Trim();
            classInfo.Code = txt_Code.Value.Trim();
            if (ShareCodeManager.SaveShareCodeClass(classInfo))
            {
                ResultNum = 1;
            }
            else
            {
                ResultNum = 3;
            }
            #endregion
        }
    }
}